/***************
 * Submission Source Panel
 */
AIR2.Submission.Source = function() {
    var srcLink = AIR2.HOMEURL + '/source/';
    var srcName = AIR2.Submission.SRCDATA.radix;
    srcName = AIR2.Format.sourceFullName(srcName);

    return new AIR2.UI.Panel({
        colspan: 1,
        title: srcName,
        iconCls: 'air2-icon-source',
        storeData: AIR2.Submission.SRCDATA,
        url: AIR2.Submission.SUBMURL + '/source.json',
        itemSelector: '.subm-source',
        tpl: new Ext.XTemplate(
            '<tpl for="."><div class="subm-source">' +
              '<ul class="meta">' +
                // email
                '<tpl for="SrcEmail">' +
                  '<li>{[AIR2.Format.sourceEmail(values, true)]}</li>' +
                '</tpl>' +
                // phone
                '<tpl for="SrcPhoneNumber">' +
                  '<li>{[AIR2.Format.sourcePhone(values)]}</li>' +
                '</tpl>' +
                // address
                '<tpl for="SrcMailAddress">' +
                  '<li>{[AIR2.Format.sourceMailShort(values)]}</li>' +
                '</tpl>' +
                // anything else!
                '{[this.formatOther(values)]}' +
                '<li class="profile"><a href="'+srcLink+'{src_uuid}"><b>View Profile&nbsp;&#187;</b></a></li>' +
              '</ul>' +
            '</div></tpl>',
            {
                compiled: true,
                disableFormats: true,
                formatOther: function(v) {
                    var str = '';
                    var count = 0;
                    var maxOther = 4;

                    // format vita experience
                    for (var i=0; i<v.SrcVita.length && count<maxOther; i++, count++) {
                        var sv = v.SrcVita[i];
                        var val = (sv.sv_value) ? sv.sv_value : '';
                        var bas = (sv.sv_basis) ? sv.sv_basis : '';
                        if (val.length > 0 && bas.length > 0) {
                            str += '<li>' + val + ' - ' + bas + '</li>';
                        }
                        else {
                            str += '<li>' + val + bas + '</li>';
                        }
                    }

                    // format facts
                    for (var i=0; i<v.SrcFact.length && count<maxOther; i++, count++) {
                        var sf = v.SrcFact[i];
                        val = sf.sf_src_value;
                        if (sf.AnalystFV) val = sf.AnalystFV.fv_value;
                        else if (sf.SourceFV) val = sf.SourceFV.fv_value;
                        str += '<li>' + sf.Fact.fact_name + ': ' + val + '</li>';
                    }

                    return str;
                }
            }
        )
    });
}